<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="用户号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusId">
              <j-dict-select-tag type="list" v-model="model.cusId"  dictCode="mws_t_arc_customers,code,id" placeholder="请选择用户号" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="用户名" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cusName">
              <a-input v-model="model.cusName" placeholder="请输入用户名" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="用水性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="natureId">
              <j-dict-select-tag type="list" v-model="model.natureId"  dictCode="mws_t_base_natures,name,id" placeholder="请选择用水性质" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readMonth">
              <a-input v-model="model.readMonth" placeholder="请输入月份" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="起度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginScale">
              <a-input-number v-model="model.beginScale" placeholder="请输入起度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="止度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endScale">
              <a-input-number v-model="model.endScale" placeholder="请输入止度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄见水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readAmount">
              <a-input-number v-model="model.readAmount" placeholder="请输入抄见水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="换表读数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="replaceMeterScale">
              <a-input-number v-model="model.replaceMeterScale" placeholder="请输入换表读数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="换表水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="replaceAmount">
              <a-input-number v-model="model.replaceAmount" placeholder="请输入换表水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="计量水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="measureAmount">
              <a-input-number v-model="model.measureAmount" placeholder="请输入计量水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="减免水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ajustedAmount">
              <a-input-number v-model="model.ajustedAmount" placeholder="请输入减免水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="计费水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amount">
              <a-input-number v-model="model.amount" placeholder="请输入计费水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="合计金额(仅水费)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalMoney">
              <a-input-number v-model="model.totalMoney" placeholder="请输入合计金额(仅水费)" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="增值税金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="vatMoney">
              <a-input-number v-model="model.vatMoney" placeholder="请输入增值税金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="优惠金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="discountMoney">
              <a-input-number v-model="model.discountMoney" placeholder="请输入优惠金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="杂项金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="alienMoney">
              <a-input-number v-model="model.alienMoney" placeholder="请输入杂项金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="折后金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="money">
              <a-input-number v-model="model.money" placeholder="请输入折后金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="违约金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="penalty">
              <a-input-number v-model="model.penalty" placeholder="请输入违约金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="违约天数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="penaltyDay">
              <a-input-number v-model="model.penaltyDay" placeholder="请输入违约天数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readerId">
              <a-input v-model="model.readerId" placeholder="请输入抄表员" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bookId">
              <j-dict-select-tag type="list" v-model="model.bookId"  dictCode="mws_t_mr_books,name,id" placeholder="请选择抄表本号" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="抄表日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="readTime">
              <a-input v-model="model.readTime" placeholder="请输入抄表日期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="水表状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="waterUsedType">
              <j-dict-select-tag type="list" v-model="model.waterUsedType"  dictCode="water_used_type" placeholder="请选择水表状态" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="已收金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recivedMoney">
              <a-input-number v-model="model.recivedMoney" placeholder="请输入已收金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="欠费金额包括违约金" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrearMoney">
              <a-input-number v-model="model.arrearMoney" placeholder="请输入欠费金额包括违约金" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="已收水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recivedAmount">
              <a-input-number v-model="model.recivedAmount" placeholder="请输入已收水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="欠费水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="arrearAmount">
              <a-input-number v-model="model.arrearAmount" placeholder="请输入欠费水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="锁定水量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lockedAmount">
              <a-input-number v-model="model.lockedAmount" placeholder="请输入锁定水量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="锁定金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lockedMoney">
              <a-input-number v-model="model.lockedMoney" placeholder="请输入锁定金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="操作日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operatedTime">
              <a-input v-model="model.operatedTime" placeholder="请输入操作日期" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="短信催缴次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remindNum">
              <a-input-number v-model="model.remindNum" placeholder="请输入短信催缴次数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="欠费明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="chgSubArrearTable.loading"
            :columns="chgSubArrearTable.columns"
            :dataSource="chgSubArrearTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ChgArrearsForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        validatorRules: {
           cusId: [
              { required: true, message: '请输入用户号!'},
           ],
           cusName: [
              { required: true, message: '请输入用户名!'},
           ],
           natureId: [
              { required: true, message: '请输入用水性质!'},
           ],
           readMonth: [
              { required: true, message: '请输入月份!'},
           ],
           beginScale: [
              { required: true, message: '请输入起度!'},
           ],
           endScale: [
              { required: true, message: '请输入止度!'},
           ],
           readAmount: [
              { required: true, message: '请输入抄见水量!'},
           ],
           replaceMeterScale: [
              { required: true, message: '请输入换表读数!'},
           ],
           replaceAmount: [
              { required: true, message: '请输入换表水量!'},
           ],
           measureAmount: [
              { required: true, message: '请输入计量水量!'},
           ],
           ajustedAmount: [
              { required: true, message: '请输入减免水量!'},
           ],
           amount: [
              { required: true, message: '请输入计费水量!'},
           ],
           totalMoney: [
              { required: true, message: '请输入合计金额(仅水费)!'},
           ],
           vatMoney: [
              { required: true, message: '请输入增值税金额!'},
           ],
           money: [
              { required: true, message: '请输入折后金额!'},
           ],
           penalty: [
              { required: true, message: '请输入违约金!'},
           ],
           readerId: [
              { required: true, message: '请输入抄表员!'},
           ],
           bookId: [
              { required: true, message: '请输入抄表本号!'},
           ],
           recivedMoney: [
              { required: true, message: '请输入已收金额!'},
           ],
           arrearMoney: [
              { required: true, message: '请输入欠费金额包括违约金!'},
           ],
           recivedAmount: [
              { required: true, message: '请输入已收水量!'},
           ],
           arrearAmount: [
              { required: true, message: '请输入欠费水量!'},
           ],
           lockedAmount: [
              { required: true, message: '请输入锁定水量!'},
           ],
           lockedMoney: [
              { required: true, message: '请输入锁定金额!'},
           ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['chgSubArrear', ],
        tableKeys:['chgSubArrear', ],
        activeKey: 'chgSubArrear',
        // 欠费明细
        chgSubArrearTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '费用代码',
              key: 'itemId',
              type: FormTypes.select,
              dictCode:"mws_t_base_charge_item,code,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '用水价格',
              key: 'priceId',
              type: FormTypes.select,
              dictCode:"mws_t_base_water_prices,price,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '用水性质',
              key: 'natureId',
              type: FormTypes.select,
              dictCode:"mws_t_base_natures,name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '阶梯号',
              key: 'tierNo',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '水量',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '增值税金额',
              key: 'vatMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '优惠金额',
              key: 'discountMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '总金额',
              key: 'totalMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '欠费水量',
              key: 'arrearAmount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '欠费金额（不包括违约金）',
              key: 'arrearMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '已收金额',
              key: 'recivedMoney',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '是否锁定',
              key: 'isLocked',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '锁定类型：1预开票，2托收',
              key: 'lockedTypeId',
              type: FormTypes.select,
              dictCode:"fee_locked_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '同步标识',
              key: 'syncFlag',
              type: FormTypes.select,
              dictCode:"yn",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '最后同步时间',
              key: 'syncDatetime',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/chg/chgArrears/add",
          edit: "/chg/chgArrears/edit",
          chgSubArrear: {
            list: '/chg/chgArrears/queryChgSubArrearByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
     addBefore(){
            this.chgSubArrearTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.chgSubArrear.list, params, this.chgSubArrearTable)
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          chgSubArrearList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>